﻿using System;
using System.Collections;
using System.Collections.Generic;
using TTGameEngine;
using UnityEngine;

public enum UIFormID {
    NoInit,
    UI_FloatMsg,
    UI_TipsMsg,
    UI_BGMsg,
    UI_Main,
}

public enum UIOpenMode {
    OnlyOne,
    /// <summary>
    /// 可同时存在多个。关闭时，需要使用closeUI(uibase)。 不可使用formID关闭。
    /// </summary>
    MultiOpened,
}

[Serializable]
public class UIConfig {
    public UIFormID formID = UIFormID.NoInit;
    public GameObject prefab = null;
    public UIOpenMode openMode = UIOpenMode.OnlyOne;
}

public class UIManager : MonoBehaviour {
    public static UIManager Ins;

    [SerializeField]
    UIConfig[] Editor_uiConfigs;
    Dictionary<UIFormID, UIConfig> m_uiconfigs = new Dictionary<UIFormID, UIConfig>();
    [SerializeField, ReadOnly]
    Dictionary<UIFormID, UIBase> m_openingUIs = new Dictionary<UIFormID, UIBase>();
    Transform tf;

    private void Awake() {
        if(Ins != null) {
            DebugUtil.LogErrorFormat("[UIManager]Dumplicate Instance!");
        }
        Ins = this;

        tf = this.transform;
        //mark:youhua  Editor脚本处理Dict存储。
        for(int i = 0; i < Editor_uiConfigs.Length; i++) {
            UIConfig config = Editor_uiConfigs[i];
            m_uiconfigs.Add(config.formID, config);
        }

        init();
    }

    // Use this for initialization
    void Start() {

    }

    // Update is called once per frame
    void Update() {

    }

    void init() {
        UIObjectRecycler.Instance.Init(transform);
    }

    public UIBase ShowUI(UIFormID uiid, UIDataBase uidata)
    {
        UIBase uibase = null;
        UIConfig uiconfig;
        if(m_uiconfigs.TryGetValue(uiid, out uiconfig)) {
            //GameObject uiObj = (GameObject)GameObject.Instantiate(uiconfig.prefab);
            GameObject uiObj = UIObjectRecycler.Instance.InstantiatePrefab(uiconfig.prefab, Vector3.zero, Quaternion.identity);
            uiObj.transform.SetParent(tf, false);
            //uiObj.transform.localPosition = Vector3.zero;
            uibase = uiObj.GetComponent<UIBase>();
            uibase.SetupUI(uiconfig, uidata);
            if(uiconfig.openMode != UIOpenMode.MultiOpened) {
                m_openingUIs.Add(uiid, uibase);
            }

            DebugUtil.LogFormat("[UIManager] Open UI[{0}]", uiid.ToString());
            uiObj.SetActive(true);
        } else {
            DebugUtil.LogErrorFormat("[UIManager]Show UI, but not found uiconfig. ui[{0}] 需要在UIManager脚本中预先配置UI。", uiid.ToString());
        }
        return uibase;
    }

    public void CloseUI(UIFormID uiid) {
        UIBase uiobj;
        if(m_openingUIs.TryGetValue(uiid, out uiobj)) {
            UIObjectRecycler.Instance.Deactivate(uiobj.gameObject);

            m_openingUIs.Remove(uiid);
            DebugUtil.LogFormat("[UIManager] Close UI[{0}]", uiid.ToString());
        } else {
            DebugUtil.LogErrorFormat("[UIManager]Close UI, but not in openning list. may be FormID is a multi UI. ui[{0}]", uiid.ToString());
        }
    }

    public void CloseUI(UIBase uiobj) {
        UIObjectRecycler.Instance.Deactivate(uiobj.gameObject);
        DebugUtil.LogFormat("[UIManager] Close UI[{0}]", uiobj.ToString());
    }

    public UIBase GetUI(UIFormID uiid) {
        UIBase uiobj = null;
        if(m_openingUIs.TryGetValue(uiid, out uiobj)) {

        } else {
            DebugUtil.LogErrorFormat("[UIMgr] GetUI fail. UIID[{0}]", uiid.ToString());
        }
        return uiobj;
    }

}
